//判断登陆设备
//#ifdef H5
var device_type = 'web'
//#endif

//#ifdef APP-PLUS
var device_type = 'mobile'
//#endif

//#ifdef MP-BAIDU
var device_type = 'wxapp'
//#endif


//#ifdef MP
var device_type = 'wxapp'
//#endif

var app = getApp();

// var host = "https://yngs.root3.cn/user_server";
var host = "http://192.168.0.106:9993/basket_user"; 
// var host = "http://221b0w4425.qicp.vip/basket_user"; 

var socketUrl = "wss://tesla.natapp1.cc"; //替换为自己的websocket地址,小程序需要wss

module.exports = {
	socketUrl: socketUrl,
	HOST: host,
	API_ROOT: host,
	DeviceType: device_type,
	post(options) {
		// console.log('post')
		// console.log(options)
		this.request(options);
	},
	delete(options) {
		options.method = 'DELETE';
		this.request(options);
	},
	get(options) {
		options.method = 'GET';
		this.request(options);
	},
	put(options) {
		options.method = 'PUT';
		this.request(options);
	},
	request(options) {
		var apiRoot = this.API_ROOT;
		var token = '';
		try {
			token = uni.getStorageSync('token')
		} catch (e) {
			// Do something when catch error
		}


		uni.request({
			url: apiRoot + options.url,
			data: options.data,
			method: options.method ? options.method : 'POST',
			header: {
				// 'Content-Type': 'application/json',
				'content-type': 'application/x-www-form-urlencoded', 
				'token': token
			},
			success: res => {
				// console.log('success====')
				var data = res.data;
				// console.log(res)
				if (data.status == 901 || data.status == 902 || data.status == 903 || data.status == 904 || data.status == 905 || data.status == 906) {

					uni.removeStorageSync('token')
					// uni.removeStorageSync('USERS_KEY')
					// console.log('ssssssssssss')
					uni.showModal({
						title: '提示',
						content: '您未登录或登录状态已过期',
						icon: 'none',
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/login'
								});
							}
						}
					});
				} else if(data.status == 907 || data.status == 908 || data.status == 998 || data.status == 999){
					uni.showToast({
					    title: data.message,
					    duration: 2000,
						icon: "none"
					});
				} else {
					options.success(data);
				}
			},
			fail: function(res,status) {
				// console.log('fail====')
				// console.log(status)
				// console.log(res)
				if (options.fail) {
					options.fail(res)
				}
			},
			complete: options.complete ? options.complete : null
		});
	},
	uploadFile(options) {
		var apiRoot = this.API_ROOT;
		options.url = apiRoot + options.url;
		options.method = options.method ? options.method : 'POST';
		// console.log('url')
		// console.log(options.url)
		let token = this.getToken();


		let that = this;
		let oldSuccess = options.success;
		options.success = function(res) {
			//console.log(res.data);
			//#ifndef MP-BAIDU
			let data = JSON.parse(res.data);
			//#endif
			//#ifdef MP-BAIDU
			let data = res.data;
			//#endif
			oldSuccess(data);
		}

		options.header = {
			'token': token
		};
		uni.uploadFile(options);

	},
	getToken() {
		var token = '';
		try {
			token = uni.getStorageSync('token')
		} catch (e) {
			// Do something when catch error
		}

		return token;
	}
};
